package com.qianniu.napi.admin.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.base.Strings;
import com.qianniu.napi.admin.entity.Jifen;
import com.qianniu.napi.admin.entity.JifenFlow;
import com.qianniu.napi.admin.mapper.JifenFlowMapper;
import com.qianniu.napi.admin.service.IJifenFlowService;
import com.qianniu.napi.common.util.ObjectUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 积分流水表
 * </p>
 *
 * @author chenxuebin
 * @since 2018-08-01
 */
@Controller
public class JifenFlowController {

    private static final Logger logger = LoggerFactory.getLogger(JifenFlowController.class);

    @Autowired
    private IJifenFlowService jifenflowService;
    @Autowired
    private JifenFlowMapper jifenFlowMapper;

    @RequiresPermissions("admin:user:view")
    @RequestMapping("admin/jifenflow/index")
    public String index() {
        return "admin/jifenflow";
    }

    @RequiresPermissions("admin:user:view")
    @RequestMapping("/admin/jifenflow/list")
    @ResponseBody
    public Page<JifenFlow> alist(@RequestParam(value = "offset", required = false) Integer offset,
                                 @RequestParam(value = "status", required = false) Integer status,
                                 @RequestParam(value = "search", required = false) String search,
                                 @RequestParam(value = "limit", required = false) Integer limit) {
//        Wrapper<JifenFlow> wrapper = new EntityWrapper<JifenFlow>();
//        int utype = ObjectUtil.getUserType();
//        if (utype < 39)
//            wrapper.eq("mpid", ObjectUtil.getMpid());
//        else if (utype < 99)
//            wrapper.eq("mid", ObjectUtil.getMid());
////        wrapper.eq("state", 1);
//        if (status != 99) wrapper.eq("jftype", status);
////        if (!Strings.isNullOrEmpty(search))
////            wrapper.like("order_no", search).or().like("remark", search).or().like("service_code", search)
////                    .or().like("address", search).or().like("linkman", search).or().like("phone", search);
//
//        wrapper.orderBy("id", false);
//        Page<JifenFlow> page = jifenflowService.selectPage(new Page<JifenFlow>(offset / limit + 1, limit), wrapper);
//        return page;
        Map map = new HashMap<String,String>();
        map.put("limit",limit);
        map.put("offset",offset);
        logger.info("=====================================offset=" + offset);
        logger.info("=====================================limit=" + limit);

        Wrapper<Jifen> wrapper = new EntityWrapper<Jifen>();
        int utype = ObjectUtil.getUserType();
        if (utype < 39) {
            map.put("mpid", ObjectUtil.getMpid());

        }else if (utype < 99) {
            map.put("mid", ObjectUtil.getMid());
        }

        if (!Strings.isNullOrEmpty(search)) {
            map.put("search", search);
        }

        Page<JifenFlow> page = new Page<JifenFlow>(offset / limit + 1, limit);
        List<JifenFlow> list = jifenFlowMapper.getJifenFlowList(map);
        page.setTotal(jifenFlowMapper.getJifenFlowCount(map));
        page.setRecords(list);
        page.setSize(list.size());

        return page;

    }

    @RequestMapping("/admin/jifenflow/add")
    public String add(@Valid JifenFlow bean, BindingResult bindingResult) {
        // 字段校验
        if (bindingResult.hasErrors()) {
            logger.debug("There are errors! {}", bindingResult);
            return null;
        }
        bean.setCreatetime(new Date());
        bean.setMid(ObjectUtil.getMid());
        bean.setMpid(ObjectUtil.getMpid());
        bean.setUid(ObjectUtil.getUid());
        bean.insert();
        return "admin/jifenflow";
    }

    @RequestMapping("/admin/jifenflow/getJifenFlowById")
    @ResponseBody
    public JifenFlow getJifenFlowById(long id) {
        JifenFlow jifenflow = jifenflowService.selectById(id);
        return jifenflow;
    }

    @RequestMapping("/admin/jifenflow/deleteOne")
    @ResponseBody
    public int deleteOne(long id) {
        boolean isSucc = jifenflowService.deleteById(id);
        return isSucc ? 1 : 0;
    }

    @RequestMapping("/admin/jifenflow/changeState")
    @ResponseBody
    public String changeState(long id, int state) {
        JifenFlow bean = new JifenFlow();
        bean.setId(id);
        bean.setState(state);
        bean.setModifytime(new Date());
        bean.setModifier(ObjectUtil.getUid());
        jifenflowService.updateById(bean);
        return "success";
    }

    @RequestMapping("/admin/jifenflow/modify")
    public String update(JifenFlow bean) {
        bean.setModifytime(new Date());
        bean.setModifier(ObjectUtil.getUid());
        boolean isSuccc = jifenflowService.updateById(bean);
        if (isSuccc) {
            return "redirect:index";
        }
        return "error";
    }

}
